package com.resou.reader.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.resou.reader.R;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Author: 萤火虫
 * E-mail:  Allensuns@163.com
 * Description:
 * CreateTime: 2019-03 -18
 **/
public class TabView extends LinearLayout {

    TextView tvUnUseredOnclick;
    TextView tvUseed;
    TextView tvPastDueOnclick;
    LinearLayout mCardViewOnclick;

    int index = 0;
    public TabViewOnclick tabViewOnclick;

    public void setTabViewOnclick(TabViewOnclick tabViewOnclick) {
        this.tabViewOnclick = tabViewOnclick;

    }

    public TabView(Context context) {
        super(context);
    }

    public TabView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.tabview, this);
        tvUnUseredOnclick = findViewById(R.id.tvUnUseredOnclick);
        tvUseed = findViewById(R.id.tvUseed);
        tvPastDueOnclick = findViewById(R.id.tvPastDueOnclick);
        mCardViewOnclick = findViewById(R.id.mCardViewOnclick);
        setLinstener();
        setViewState(index);
    }

    public TabView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public synchronized TabView setPosition(int pos) {
        this.index = pos;
        setViewState(index);
        return this;
    }


    /**
     * 设置选中视图样式
     *
     * @param i 下标
     */
    private synchronized void setViewState(int i) {
        index = i;
        switch (i) {
            case 0:
                tvUnUseredOnclick.setTextColor(getResources().getColor(R.color.white));
                tvUnUseredOnclick.setBackgroundResource(R.drawable.shap_tabviewleftradius);
                tvUseed.setTextColor(getResources().getColor(R.color.theme_color));
                tvUseed.setBackgroundColor(0);
                tvPastDueOnclick.setTextColor(getResources().getColor(R.color.theme_color));
                tvPastDueOnclick.setBackgroundColor(0);
                break;
            case 1:
                tvUnUseredOnclick.setTextColor(getResources().getColor(R.color.theme_color));
                tvUnUseredOnclick.setBackgroundColor(0);
                tvUseed.setTextColor(getResources().getColor(R.color.white));
                tvUseed.setBackgroundColor(getResources().getColor(R.color.theme_color));
                tvPastDueOnclick.setTextColor(getResources().getColor(R.color.theme_color));
                tvPastDueOnclick.setBackgroundColor(0);
                break;
            case 2:
                tvUnUseredOnclick.setTextColor(getResources().getColor(R.color.theme_color));
                tvUnUseredOnclick.setBackgroundColor(0);
                tvUseed.setTextColor(getResources().getColor(R.color.theme_color));
                tvUseed.setBackgroundColor(0);
                tvPastDueOnclick.setTextColor(getResources().getColor(R.color.white));
                tvPastDueOnclick.setBackgroundResource(R.drawable.shap_tabviewrightradius);
                break;
        }
    }

    public void setLinstener() {

        tvUseed.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setViewState(1);
                if (null != tabViewOnclick) {
                    tabViewOnclick.setTabViewPositionOnclickListener(index);
                }
            }
        });

        tvPastDueOnclick.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setViewState(2);
                if (null != tabViewOnclick) {
                    tabViewOnclick.setTabViewPositionOnclickListener(index);
                }
            }
        });

        tvUnUseredOnclick.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                setViewState(0);
                if (null != tabViewOnclick) {
                    tabViewOnclick.setTabViewPositionOnclickListener(index);
                }
            }
        });

    }

    public interface TabViewOnclick {
        void setTabViewPositionOnclickListener(int pos);
    }
}
